﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace IGGCore
{
    public class RefParser : BaseParser
    {
        public void ParseStatistics(Scoreboard scoreboard)
        {
            scoreboard.StatsNLBatting = GetStatsFromOnlineSource(CoreConstants.STAT_BBREF_NLBAT);
        }

        /// <summary>
        /// Get stats from online source
        /// </summary>
        private List<StatItem> GetStatsFromOnlineSource(string url)
        {
            List<StatItem> items = new List<StatItem>();
            StreamReader reader = null;
            string statsContent = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                reader = new StreamReader(request.GetResponse().GetResponseStream());
                statsContent = reader.ReadToEnd();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            int tableIndex = statsContent.IndexOf("<table class=\"wide_table\"");
            while (tableIndex > -1)
            {
                StatItem item = new StatItem();
                int endindex = statsContent.IndexOf("</table>", tableIndex);
                string tableContent = statsContent.Substring(tableIndex, endindex - tableIndex);
                tableContent = tableContent.Substring(tableContent.IndexOf("<span class"));
                int endStrong = tableContent.LastIndexOf("</strong>");

                string lineSubject = tableContent.Substring(0, endStrong);
                lineSubject = lineSubject.Substring(lineSubject.LastIndexOf(">") + 1);
                lineSubject = lineSubject.Replace("&amp;", "&");

                tableContent.Substring(endStrong + 9);
                tableContent = tableContent.Substring(tableContent.IndexOf("<tr"));
                tableContent = tableContent.Replace("<tr class=\"bold_text\">", "");
                tableContent = tableContent.Replace("<tr class=\"\">", "");
                tableContent = tableContent.Replace("<td align=right>", "");
                tableContent = tableContent.Replace("</small></td>", "");
                tableContent = tableContent.Replace("</td><td>", "");
                tableContent = tableContent.Replace("</td></tr>", "@");
                int playerIndex = tableContent.IndexOf("title");
                while (playerIndex > -1)
                {
                    int playerEndindex = tableContent.IndexOf(">", playerIndex) - 1;
                    int valueStart = tableContent.IndexOf("<small>", playerIndex) + 7;
                    int valueEnd = tableContent.IndexOf("@", valueStart);

                    string teamValue = tableContent.Substring(valueStart, valueEnd - valueStart);
                    item.Teams.Add(teamValue.Substring(1, 3));
                    item.Values.Add(teamValue.Substring(23));
                    item.Names.Add(tableContent.Substring(playerIndex + 7, playerEndindex - (playerIndex + 7)));

                    playerIndex = tableContent.IndexOf("title", playerEndindex);
                }

                tableIndex = statsContent.IndexOf("<table class=\"wide_table\"", endindex);
                item.Subject = lineSubject;
                items.Add(item);
            }
            return items;
        }

    }
}
